package com.honeywen.mymvc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangwei
 * @date 2019/11/14
 */
@Slf4j
@Controller
@RequestMapping("/data")
public class DataController {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> save() {


        String sql = "select * from dict";
        List<Map<String, Object>> list = jdbcTemplate.query(sql, new RowMapper<Map<String, Object>>() {
            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                HashMap<String, Object> map = new HashMap<>();
                int id = rs.getInt("id");
                int num = rs.getInt("num");
                String name = rs.getString("name");
                map.put("id", id);
                map.put("num", num);
                map.put("name", name);
                return map;
            }
        });


        log.info("<--data-->{}", list);



        return list;
    }


}
